var html, fld, s;
var pxc = 1;
var html_begin, html_end;
var fldArray_to = new Array;
var fldArray_from = new Array;

Array.prototype.inArray = function (value){
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

function doTheFiltering(self,sVar,aBadWords) {
	if(typeof(aBadWords)=='undefined')aBadWords=new Array();
	var href=window.location.search.split('&');
	var sHref= new String(window.location.pathname);
	for (i=0; i<href.length; i++){	if (href[i].substr(0,sVar.length)!=sVar && !aBadWords.inArray(href[i].split('=')[0])) sHref+=href[i]+'&';	}			
	window.location.href=sHref+sVar+'='+self.value;
}

function checkboxStatus(sArrayName,bStatus) {				
	var oInputs=document.getElementsByTagName('INPUT');
	var i;
	for(i in oInputs) {
		if (typeof(oInputs[i].name)!='undefined' && oInputs[i].name.substr(0,sArrayName.length)==sArrayName) oInputs[i].checked=bStatus;	
	}
}

function OrChek(form){
	var bodys = window.document.forms[form];
	for (var index = 0; index < bodys.length; index++) {
		if (bodys.elements[index].type == 'checkbox'){
			if (bodys.elements[index].checked == true) {bodys.elements[index].value = '1';}
			else {bodys.elements[index].value = '0';}
		}
	}
}

function DocumEditor(){
	this.SetTemp = function(el){
		this.myTempArea = el;
	}
	this.GetClipboardHTML = function(format){
		var range = this.myTempArea.createTextRange();
		range.execCommand("SelectAll");
		range.execCommand("Paste");
		var sData = this.myTempArea.document.body;
		return sData;
	}
}

var DocumEditor = new DocumEditor();

function AddArray(fld, s){
	if (s == 0) fldArray_to.push(fld);
	if (s == 1) fldArray_from.push(fld);
}

function doSubmit(frm)
{	
	for (var i=0; i<fldArray_to.length; i++) {		
		ShowNormal(fldArray_from[i]);
		document.all[fldArray_from[i]].style.visibility='hidden';		
		normalizeSizes(fldArray_from[i]);
		window.document.forms[frm].elements[fldArray_to[i]].value=document.all[fldArray_from[i]].DOM.body.innerHTML;
	}
	
	/*
	if (fldArray_to.length == 0){
		var bodys = window.document.forms[frm];
		for (var index = 0; index < bodys.length; index++) {
			if (bodys.elements[index].tagName.toLowerCase() == 'textarea'){
				var fld = document.getElementById(bodys.elements[index].id);
				var d = window.document.createElement('DIV');
				d.innerHTML = fld.value;
				var tmp = d.getElementsByTagName("IMG");
				for (var i = 0; i < tmp.length; i++) {
					tag = tmp[i];
					if(tag.style.width!='' && tag.style.width!=''){				
						tag.src=tag.src.replace(tag.src.match(":[w|h]\.*"),':w.'+tag.style.width.toString().replace('px','')+':h.'+tag.style.height.toString().replace('px',''));	
					}else if (tag.width!='' && tag.height!='') {
						tag.src=tag.src.replace(tag.src.match(":[w|h]\.*"),':w.'+tag.width+':h.'+tag.height);			
					}	
					tag.removeAttribute('width');
					tag.removeAttribute('height');
					tag.removeAttribute('style');
				}
				document.forms[frm].elements[index].value = d.innerHTML;
			}
		}
	}
	*/
	
	document.forms[frm].submit();				
}

function ReplaseTagPToBr (fld)
{
	var body = fld.DOM.body;	
	for (var index = 0; index < body.all.length; index++) {
		tag = body.all[index];
		if(tag.tagName.toLowerCase()=='p'){
			tag.outerHTML = tag.innerHTML + '<BR>';
		}
	}	
}

function insertFld (fld, html)
{
	placewhere = document.all[fld].DOM.selection.createRange();	
	if(!html) return false;	
	placewhere.pasteHTML(html);
	placewhere.select();
}

function insertData (fld, html)
{
	placewhere = document.all[fld].DOM.selection.createRange();	
	if(!html) return false;	
	placewhere.pasteHTML(html);
	placewhere.select();
}

function insertImagePopUp (fld, html)
{
		if(!html) return false;
		if (document.all[fld].DOM.selection.type.toLowerCase() == 'text'){
			document.all[fld].ExecCommand(5050);
			placewhere = document.all[fld].DOM.selection.createRange();	
			html = html + document.all[fld].DOM.selection.createRange().htmlText + "</a>";
			placewhere.pasteHTML(html);
			placewhere.select();
		} else if (document.all[fld].DOM.selection.type.toLowerCase() == 'control'){
			document.all[fld].ExecCommand(5050);
			var oControlRange = document.all[fld].DOM.selection.createRange();	
			if(oControlRange.item(0).tagName=="IMG"){
				html = html + document.all[fld].DOM.selection.createRange().item(0).outerHTML + "</a>";
			}
			document.all[fld].DOM.selection.createRange().item(0).outerHTML = html;
		} else {
			var cControl = document.all[fld].DOM.selection.createRange();
			tControl = cControl.parentElement();
			var temp = tControl.innerHTML;
			if (tControl.tagName.toLowerCase()== 'a'){
				placewhere = cControl;
				tControl.parentNode.removeChild(tControl);
				html = html + temp + "</a>";
				placewhere.pasteHTML(html);
				placewhere.select();
			}
		}
}

function HtmlWarp (fld, html_begin, html_end)
{
		if (!html_begin && !html_end) return false;
		var Range = document.all[fld].DOM.selection.createRange();
		if (document.all[fld].DOM.selection.type.toLowerCase() == 'text'){
			html = html_begin + Range.htmlText + html_end;
			Range.pasteHTML(html);
			Range.select();
		}else if (document.all[fld].DOM.selection.type.toLowerCase() == 'control'){
			html = html_begin + Range.item(0).outerHTML + html_end;
			Range.item(0).outerHTML = html;
		}
}

function normalizeSizes (fld)
{
	var body = document.all[fld].DOM.body;	
	for (var index = 0; index < body.all.length; index++) {
		tag = body.all[index];
		if(tag.tagName.toLowerCase()=='img'){			
			if(tag.style.width!='' && tag.style.width!=''){				
				tag.src=tag.src.replace(tag.src.match(":[w|h]\.*"),':w.'+tag.style.width.toString().replace('px','')+':h.'+tag.style.height.toString().replace('px',''));	
			}else if (tag.width!='' && tag.height!='') {
				tag.src=tag.src.replace(tag.src.match(":[w|h]\.*"),':w.'+tag.width+':h.'+tag.height);			
			}	
			tag.removeAttribute('width');
			tag.removeAttribute('height');
			tag.removeAttribute('style');				
		} else if (tag.tagName.toLowerCase()=='table') {
			tag.removeAttribute('width');
			tag.removeAttribute('height');
			tag.setAttribute('width','100%',0);
			tag.setAttribute('cellpadding','0',0);
			tag.setAttribute('cellspacing','0',0);
			tag.setAttribute('border','1',0);
		}
		
	}	
}

function InsertSup(fld)
{
	if (document.all[fld].DOM.selection.createRange().text.length > 0) {
		placewhere = document.all[fld].DOM.selection.createRange();	
		html = "<sup>" + document.all[fld].DOM.selection.createRange().text + "</sup>";
		placewhere.pasteHTML(html);
		placewhere.select();
	}
}

function InsertSub(fld)
{
	if (document.all[fld].DOM.selection.createRange().text.length > 0) {
		placewhere = document.all[fld].DOM.selection.createRange();	
		html = "<sub>" + document.all[fld].DOM.selection.createRange().text + "</sub>";
		placewhere.pasteHTML(html);
		placewhere.select();
	}
}

function StyleClassChange(fld, sel_obj)
{ 
	var classname = sel_obj.options[sel_obj.selectedIndex].value;
	var cRange = document.all[fld].DOM.selection.createRange();
	var parentTd = cRange.parentElement();
	if (parentTd != null && parentTd.tagName.toLowerCase() != 'body'){

		if ((sel_obj.options.value != 0) && (sel_obj.options.value != -1)) parentTd.className = classname;
		else parentTd.removeAttribute('className');
		
	} else if (parentTd.tagName.toLowerCase() == 'body'){
		var body = document.all[fld].DOM.body.innerHTML;
		if (classname != 'default') body = '<span class="'+classname+'">'+body+'</span>';
		else body = '<span>'+body+'</span>';
	}

	sel_obj.options[0].selected = true;
	return 0;
}

function FontColorChange(fld, sel_obj)
{
	var color = sel_obj.options[sel_obj.selectedIndex].value;
	var SelectN = document.all[fld].DOM.selection;
	var cRange = SelectN.createRange();	
	var parentTd = cRange.parentElement();
	if (SelectN.type.toLowerCase() == 'text'){
		var text = cRange.htmlText.replace('\n','');
		if ((sel_obj.options.value != 0) && (sel_obj.options.value != -1)) {
			if (parentTd.innerHTML == cRange.htmlText){
				parentTd.style.color = "#"+sel_obj.options.value;
				sel_obj.options[0].selected = true;
			} else {
				html = "<font color=#" + color + ">" + cRange.htmlText + "</font>";
				cRange.pasteHTML(html);
				cRange.select();
			}
		} else if (sel_obj.options.value == -1){
			parentTd.style.color = "";
			sel_obj.options[0].selected = true;
		}
	}
	sel_obj.options[0].selected = true; 
	return 0;
}

function CellBgColorChange(fld, sel_obj)
{ 
	var cRange = document.all[fld].DOM.selection.createRange();
	var parentTd = cRange.parentElement();
	if (parentTd.tagName.toLowerCase() == 'td'){
		if ((sel_obj.options.value != 0) && (sel_obj.options.value != -1)) {
			parentTd.style.backgroundColor = sel_obj.options.value;
			sel_obj.options[0].selected = true;
		} else if (sel_obj.options.value == -1){
			parentTd.style.backgroundColor = "";
			sel_obj.options[0].selected = true;
		}
		sel_obj.options[0].selected = true;
		return 0;
	} else while ((parentTd.tagName.toLowerCase() != 'td') && (parentTd.tagName.toLowerCase() != 'th') && (parentTd.tagName.toLowerCase() != 'table') && (parentTd.tagName.toLowerCase() != 'body')) {
		var parentTd = parentTd.parentElement;		
		if ((parentTd.tagName.toLowerCase() == 'td')) {
			if ((sel_obj.options.value != 0) && (sel_obj.options.value != -1)) {
				parentTd.style.backgroundColor = sel_obj.options.value;
				sel_obj.options[0].selected = true;
			} else if (sel_obj.options.value == -1){
				parentTd.style.backgroundColor = "";
				sel_obj.options[0].selected = true;
			}
			sel_obj.options[0].selected = true;
			return 0;
		}
	}
	sel_obj.options[0].selected = true;
	return 0;
}

function ShowHTML(fld) {
	if (pxc != 2) {
		document.all[fld].DOM.body.innerText = document.all[fld].DOM.body.innerHTML 
		document.all[fld].focus()
		pxc = 2
	}
}

function ShowNormal(fld) {
	if (pxc != 1) {
		document.all[fld].DOM.body.innerHTML = document.all[fld].DOM.body.innerText;
		document.all[fld].focus()
		pxc = 1
	}
}

function editor_insertTable(rows, cols, border, attrib){
	if (border.checked) border = " class='tvs_core_table_border_style_on'";
	else border = " class='tvs_core_table_border_style_off'";
	TD = cols;
	CTD = "";
	for (i=0;i<TD;i++) {
		CTD = CTD+"<td> </td>";
	}
	TR = rows;
	CTR = '';
	for (i=0;i<TR;i++) {
		CTR = CTR+"<tr>"+CTD+"</tr>";
	}	 	
	return "<table "+attrib+border+">"+CTR+"</table>";	
}

function CleanEmptyLinks(editdoc){
var oCCC = editdoc.DOM;
	for (i=0; i<oCCC['links'].length; i++) {
		if (oCCC['links'][i].innerHTML == ''){
			oCCC['links'][i].outerHTML = '';
		}
	}
}

function cleanCode(editdoc)
{
	var body = document.all[editdoc].DOM.body;
	for (var index = 0; index < body.all.length; index++) {
		tag = body.all[index];
		tag.removeAttribute("className","",0);
		tag.removeAttribute("style","",0);
		tag.removeAttribute("lang","",0);
		tag.removeAttribute("width","",0);
		tag.removeAttribute("face","",0);
	}
	var html = body.innerHTML;
	html = html.replace(/<o:p>*<\/o:p>/gi, "");
	html = html.replace(/o:/gi, "");
	html = html.replace(/x:str/gi, "");
	html = html.replace(/<st1:.*?>/gi, "");
	html = html.replace(/<DIV>/gi, '');
	html = html.replace(/<\/DIV>/gi, '<BR>');
	html = html.replace(/<SPAN>/gi, '');
	html = html.replace(/<FONT.*?>/gi, '');
	html = html.replace(/<\/FONT>/gi, '');
	html = html.replace(/<\/SPAN>/gi, '');
	html = html.replace(/<P><\/P>/gi, "");
    html = html.replace(/<COL.*>/gi, '');
	document.all[editdoc].DOM.body.innerHTML = html;
}

function cleanCodeWord(fld){
	var bodys = DocumEditor.GetClipboardHTML('body');
	for (var index = 0; index < bodys.all.length; index++) {
		tag = bodys.all[index];
		tag.removeAttribute("className","",0);
		tag.removeAttribute("style","",0);
		tag.removeAttribute("lang","",0);
		tag.removeAttribute("width","",0);
	}
	var html = bodys.innerHTML;
	html = html.replace(/<o:p>*<\/o:p>/gi, "");
	html = html.replace(/o:/gi, "");
	html = html.replace(/x:str/gi, "");
	html = html.replace(/<st1:.*?>/gi, "");
	html = html.replace(/<SPAN>/gi, '');
	html = html.replace(/<\/SPAN>/gi, '');
	html = html.replace(/<P>&nbsp;<\/P>/gi, '');
	html = html.replace(/<P><\/P>/gi, '');
	html = html.replace(/<COL.*>/gi, '');

	placewhere = document.all[fld].DOM.selection.createRange();	
	placewhere.pasteHTML(html);
	placewhere.select();
}

function s_confirm (text,yes,no)
{
	
	var name = confirm(text)
	if (name == true)
	{
		if (yes=='') return false;
		else window.location=yes;
	}
	else
	{
		if (no=='')return false;
		else window.location=no;
	}
}

/*******/

function titleBlockOut(oBlock,sBlockTitle){
	oBlock.style['backgroundColor']='#EDF0F2';
	document.getElementById(sBlockTitle).style['backgroundColor']='#667983'
}
function titleBlockHover(oBlock,sBlockTitle){
	oBlock.style['backgroundColor']='#ffffff';
	document.getElementById(sBlockTitle).style['backgroundColor']='#25404E'
}
